package com.crm.system.service.impl.activityManager;

import com.crm.model.entity.activityManager.CustomerProductActivityRelation;
import com.crm.model.entity.productManager.CustomerMemberProduct;
import com.crm.model.entity.productManager.CustomerProduct;
import com.crm.service.activityManager.CustomerProductActivityRelationDbService;
import com.crm.service.productManager.CustomerMemberProductDbService;
import com.crm.service.productManager.CustomerProductDbService;
import com.crm.system.service.activityManager.CustomerProductActivityRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author luojie
 * @date 2021/2/24 14:43
 */
@Service
public class CustomerProductActivityRelationServiceImpl implements CustomerProductActivityRelationService {

    @Autowired
    private CustomerProductActivityRelationDbService customerProductActivityRelationDbService;
    @Autowired
    private CustomerProductDbService customerProductDbService;
    @Autowired
    private CustomerMemberProductDbService customerMemberProductDbService;

    @Override
    public List<HashMap> selectProductDropDownNotSelectedListByActivityId(String activityId,String productName) {
        List<HashMap> customerProductList = customerProductDbService.selectALLProductDropDownList(productName);
        List<HashMap> hashMapList = new ArrayList<HashMap>();
        //List<CustomerProduct> customerProductList = customerProductDbService.selectProductDropDownList("");
        List<CustomerProductActivityRelation> customerProductActivityRelationList =  customerProductActivityRelationDbService.selectProductDropDownSelectedListByActivityId(activityId);
        customerProductList.stream().forEach(customerProduct -> {
            if(!customerProductActivityRelationList.stream().anyMatch(customerProductActivityRelation -> customerProductActivityRelation.getCustomerProductId().equals(customerProduct.get("productId")))){
                HashMap hashMap = new HashMap();
                hashMap.put("productId",customerProduct.get("productId"));
                hashMap.put("productName",customerProduct.get("productName"));
                hashMap.put("productCategories",customerProduct.get("productCategories"));
                hashMapList.add(hashMap);
            }
        });
        return hashMapList;
    }

    @Override
    public List<HashMap> selectProductDropDownSelectedListByActivityId(String activityId) {
        List<HashMap> hashMapList = new ArrayList<HashMap>();
        List<CustomerProductActivityRelation> customerProductActivityRelationList =  customerProductActivityRelationDbService.selectProductDropDownSelectedListByActivityId(activityId);
        customerProductActivityRelationList.stream().forEach(customerProductActivityRelation -> {
            HashMap hashMap = new HashMap();
            if("1".equals(customerProductActivityRelation.getProductCategories())){
                String productId = customerProductActivityRelation.getCustomerProductId();
                CustomerMemberProduct customerMemberProduct = customerMemberProductDbService.selectMemberProductById(productId);
                hashMap.put("productId",productId);
                hashMap.put("productName",customerMemberProduct.getMemberCardName());
                hashMap.put("productCategories","1");
                hashMap.put("productFinalPrice",customerProductActivityRelation.getProductFinalPrice());
            }else{
                String productId = customerProductActivityRelation.getCustomerProductId();
                CustomerProduct customerProduct = customerProductDbService.selectCustomerProductByProductId(productId);
                hashMap.put("productId",productId);
                hashMap.put("productName",customerProduct.getProductName());
                hashMap.put("productCategories","0");
                hashMap.put("productFinalPrice",customerProductActivityRelation.getProductFinalPrice());
            }
            hashMapList.add(hashMap);
        });
        return hashMapList;
    }
}
